<?php

namespace App\Admin\Controllers\news;

use App\Common\statement\CommonStatement;
use App\Models\news\News;
use App\Models\news\NewsCate;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class NewsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '资讯';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new News());

        $grid->model()->latest();
        $grid->disableExport();
//        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {
                $filter->like('title',__('Title'));
            });

            $filter->column(1/3, function ($filter) {
//                $filter->equal('posts_cate_id',__('Posts cate id'))->select(PostsService::getCate());
            });

            $filter->column(1/3, function ($filter) {
                $filter->between('created_at',__('Created at'))->datetime();
                $filter->equal('type',__('Type'))->select(NewsCate::where('status',1)->where('pid',0)->pluck('name','id'));
            });

        });

        $grid->column('id',__('ID'));
        $grid->column('title', __('Title'));
        $grid->column('type', __('Type'))->display(function ($type){
                return NewsCate::where('status',1)->where('pid',0)->pluck('name','id')[$type];
        });

        $grid->column('cate.name', __('Cate'));

        $grid->column('label',__('Label'))->display(function ($label){
            return CommonStatement::cover_type[$label];
        });
        $grid->column('tag',__('Tag'));
        $grid->column('status',__('Status'))->display(function ($status){
            return CommonStatement::allow_status[$status];
        });;
        $grid->column('publish_time',__('Publish Time'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(News::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('cate_id', __('Cate'));
        $show->field('type', __('Type'));
        $show->field('title', __('Title'));
        $show->field('content', __('Content'))->editor();
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new News());
        $form->text('title',__('Title'))->required();
        $form->text('tag',__('Tag'))->required();
        $form->select('type', __('Type'))
            ->options(
               NewsCate::where('pid',0)->where('status',1)->pluck('name','id')
            )->when(1, function (Form $form) {
                $form->select('cate_id', __('Cate'))
                    ->options(NewsCate::where('pid',1)
                        ->where('status',1)
                        ->pluck('name','id'));
            })->when(2, function (Form $form) {
                $form->select('cate_id', __('Cate'))
                    ->options(NewsCate::where('pid',2)
                        ->where('status',1)
                        ->pluck('name','id'));
            })->default(1);
        $form->select('label',__('Label'))
            ->options(CommonStatement::cover_type)
            ->when('=',3,function (Form $form){
                $form->file('cover_url',__('封面视频'));
            })
            ->when('=',2,function (Form $form){
                $form->image('cover_url',__('封面图片'));
            })->default(1);
        $form->editor('content');
        $form->radio('recommend',__('推荐'))->options([
            '1' => '是',
            '0' => '否'
        ])->default(1);
        $form->radio('status',__('Status'))->options(CommonStatement::allow_status)->default(1);
        $form->saving(function (Form $form) {
            if($form->status == 1){
                $form->model()->publish_time = date("Y-m-d H:i:s");
            }
        });
        return $form;
    }
}
